﻿using Himp.Base;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊挂号SagaManager
    /// </summary>
    public class OtpRgstSagaManager : OutpatientAppService
        //, IDistributedEventHandler<OtpRgstPatnBasInfoCreateRpV1Cto>
        //, IDistributedEventHandler<OrdInfoCreateRpV1Cto>
    {
        private readonly IDistributedEventBus _distributedEventBus;
        private readonly IRepository<OtpRgstRec, Guid> _optRegRepository;

        public OtpRgstSagaManager(IDistributedEventBus distributedEventBus
            , IRepository<OtpRgstRec, Guid> optRegRepository)
        {
            _distributedEventBus = distributedEventBus;
            _optRegRepository = optRegRepository;
        }

        /// <summary>
        /// 门诊挂号记录创建
        /// </summary>
        /// <param name="cto"></param>
        /// <returns></returns>
        public async Task OtpRgstRecCreate(OtpRgstRec input)
        {
            //排班锁号


            //创建订单
            var ordInfoCreate = new
            {
                BizSouc = EnumBusinessSource.门诊挂号,
                Invono = "-",
                InvoAmt = "8",
                TotAmt = "8",
                DiscAmt = "8",
                RecAmt = "8",
                OrdDetlInfos = new List<object>()
            };

            ordInfoCreate.OrdDetlInfos.Add(new
            {
                MedChrgitmTypeCode = "",
                MedChrgitmTypeName = "",
                PubItemCodg = "",
                PubItemName = "",
                Pric = 0,
                DiscRate = 1,
                Cnt = 0,
                Amt = 0
            });

            OrdInfoCreateV1Cto ordInfoCreateV1Cto = new OrdInfoCreateV1Cto();
            ordInfoCreateV1Cto.MsgCont = JsonConvert.SerializeObject(ordInfoCreate);
            await _distributedEventBus.PublishAsync(ordInfoCreateV1Cto);

            //var cto = ObjectMapper.Map<OtpRgstRec, OtpRgstPatnBasInfoCreateV1Cto>(input);
            //
        }

        /// <summary>
        /// 创建订单RP
        /// </summary>
        /// <param name="eventData"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        //[NonAction]
        private async Task HandleEventAsync(OrdInfoCreateRpV1Cto eventData)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 门诊挂号创建事件处理
        /// </summary>
        /// <param name="eventData"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        //[NonAction]
        private async Task HandleEventAsync(OtpRgstPatnBasInfoCreateRpV1Cto eventData)
        {
            try
            {
                var optReg = await _optRegRepository.GetAsync(eventData.OtpRgstId);
                optReg.OtpNo = eventData.OtpNo;
                optReg.PatnId = eventData.PatnId;
                await _optRegRepository.UpdateAsync(optReg);
            }
            catch (Exception e)
            {

            }
        }

        ///// <summary>
        ///// 患者基本信息创建完成事件处理
        ///// </summary>
        ///// <param name="eventData"></param>
        ///// <returns></returns>
        ///// <exception cref="NotImplementedException"></exception>
        //public async Task HandleEventAsync(OtpRegPatnBasInfoFinCto eventData)
        //{
        //    var optReg = await _optRegRepository.GetAsync(eventData.MsgId);
        //    optReg.PatnId = eventData.PsnId;
        //    optReg.OtpNo = eventData.OtpNo;
        //    await _optRegRepository.UpdateAsync(optReg);
        //}
    }
}
